package cn.com.nes.site.service.system.user;

import cn.com.nes.mybatis.agent.system.entity.TUser;
import cn.com.nes.site.config.CustomConfigurationProperties;
import cn.com.nes.site.entity.bo.user.PushCallCenterUserInfoBO;
import cn.com.nes.site.entity.bo.user.PushEasyRepairUserInfoBO;
import cn.com.nes.site.entity.vo.user.PushUserInfoVO;
import cn.com.nes.site.service.system.callCenter.CallCenterService;
import cn.com.nes.site.service.system.easyRepair.EasyRepairService;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推送用户账号信息抽象类
 *
 * @Author: ShenWei
 * @Date: 2024/05/24/15:01
 */
public abstract class PushUserInfoService {

    @Resource
    protected EasyRepairService easyRepairService;

    @Resource
    protected CallCenterService callCenterService;

    @Resource
    protected SystemUserAccountService systemUserAccountService;

    protected TUser userInfo;

    protected PushUserInfoVO vo;

    protected String uuid;

    protected String password;

    protected String userType; // 用户职务编码,关联t_param表 param_type=96

    protected String easyRepairUserId;

    protected String callCenterUserId;

    public abstract Integer getPushType();

    // 1、校验用户账号职务
    protected Map<String, Boolean> checkUserType() throws Exception {
        if (StrUtil.isBlank(this.userType)) {
            throw new Exception("userType为空,无需创建易修、客服中心用户账号");
        }
        // 维护用户职务编码数组
        // 01-维修 02-安装 04-管理 05-客服 08-安装维修
        List<String> easyRepairUserTypeList = Arrays.asList("01", "02", "08"); // 易修
        List<String> callCenterUserTypeList = Arrays.asList("01", "02", "04", "05", "08"); // 客服中心

        boolean pushEasyRepair = easyRepairUserTypeList.contains(this.userType);
        boolean pushCallCenter = true; // 2024-06-26 16:20 项目组决定给每一个账号都创建对应的长效管护账号
//        boolean pushCallCenter = callCenterUserTypeList.contains(this.userType);

        Map<String, Boolean> map = new HashMap<>();
        map.put("pushEasyRepair", pushEasyRepair);
        map.put("pushCallCenter", pushCallCenter);
        return map;
    }

    // 2、钩子方法 - 推送用户账号信息
    abstract void pushUserInfo(Map<String, Boolean> checkResult) throws Exception;

    // 初始化参数
    public void init(TUser userInfo, PushUserInfoVO vo) {
        this.userInfo = userInfo;
        this.vo = vo;
        this.uuid = vo.getUuid();
        this.password = StrUtil.isNotBlank(vo.getPassword()) ? vo.getPassword() : CustomConfigurationProperties.getUserDefaultPassword();
        this.userType = userInfo.getUserType();
        this.easyRepairUserId = StrUtil.EMPTY;
        this.callCenterUserId = StrUtil.EMPTY;
    }

    // 3、模板方法
    public void handle(TUser userInfo, PushUserInfoVO vo) throws Exception {
        init(userInfo, vo);

        pushUserInfo(checkUserType());
    }

    /**
     * 绑定热链云和易修账号
     *  不能判断是否已绑定易修，因为修改用户账号可能修改电话号码，不调易修接口无法判断
     */
    protected void bindEasyRepairAccount() {
        PushEasyRepairUserInfoBO easyRepairUserInfoBo = new PushEasyRepairUserInfoBO();
        easyRepairUserInfoBo.setUsername(userInfo.getPhone());
        easyRepairUserInfoBo.setPassword(userInfo.getPassword());
        easyRepairUserInfoBo.setNickname(userInfo.getNickName());
        easyRepairUserInfoBo.setPhone(userInfo.getPhone());
        easyRepairUserInfoBo.setSystemFrom("HCC");
        easyRepairUserInfoBo.setUserType(userType);

        easyRepairUserId = easyRepairService.pushEasyRepairUser(easyRepairUserInfoBo);

        systemUserAccountService.bindEasyRepairAccount(uuid, easyRepairUserId);
    }

    /**
     * 解绑热链云和易修账号
     */
    protected void unBindEasyRepairAccount() {
        systemUserAccountService.bindEasyRepairAccount(uuid, null);
    }

    /**
     * 绑定热链云和长效管护账号
     */
    protected void bindCallCenterAccount() throws Exception {
        // 创建长效管护相关用户账号 - 不在热链云分职务
        PushCallCenterUserInfoBO callCenterUserInfoBo = new PushCallCenterUserInfoBO();
        callCenterUserInfoBo.setName(userInfo.getNickName());
        callCenterUserInfoBo.setAccount(userInfo.getUsername());
        callCenterUserInfoBo.setPassword("CC_" + userInfo.getPhone());
        callCenterUserInfoBo.setCompanyId(Long.valueOf(vo.getCallCenterCompanyId()));
        callCenterUserInfoBo.setAgentUserId(uuid);
        callCenterUserInfoBo.setErUserId(easyRepairUserId);
        callCenterUserInfoBo.setUserType(userType);
        callCenterUserInfoBo.setPhone(userInfo.getPhone());

        callCenterUserId = callCenterService.pushCallCenterUser(callCenterUserInfoBo);

        systemUserAccountService.bindCallCenterAccount(uuid, callCenterUserId);
    }
}
